/**
 * 链表储存的元素类
 */
class Node {
    constructor(name) {
        this.name = name
        this.next = null
    }
}

/**
 * 单向链表类
 */
class LinkedList {
    constructor() {
        this.head = null
        this.size = 0
    }

    /**
     * 添加元素
     * @param node
     */
    add(node) {
        if (this.size === 0) {
            this.head = node
        } else {
            this.assignment(this.head, node)
        }
        this.size++
    }

    /**
     * 赋值方法
     * @param head
     * @param node
     */
    assignment(head, node) {
        if (head.next) {
            this.assignment(head.next, node)
        } else {
            head.next = node
        }
    }

    /**
     * 删除元素
     * @param index
     */
    remove(index) {
        if (index < 0 || index >= this.size) {
            throw new Error("链表越界异常")
        }
        if (index === 0) {
            this.head.next && (this.head = this.head.next)
        } else {
            let deleteNode = this.head
            let preNode = null
            let newNext = null
            for (let i = 0; i < index; i++) {
                if (i === index - 1) {
                    preNode = deleteNode
                    newNext = deleteNode?.next?.next
                }
                deleteNode = deleteNode.next
            }
            // console.log("pre", preNode)
            // console.log("delete", deleteNode)
            // console.log("newNext", newNext)
            preNode.next = newNext
        }
        this.size--
    }

}

/**
 * 队列的类
 */
class MyQueue {
    constructor() {
        this.list = new LinkedList()
    }

    add(name) {
        this.list.add(new Node(name))
        return this
    }

    remove(index) {
        this.list.remove(index)
        return this
    }

}


const myQueue = new MyQueue();
myQueue.add("A")
myQueue.add("B")
myQueue.add("C")
myQueue.add("D")
console.log(myQueue.remove(2))
